package com.kyc.kycxkplaygame.dbutils;

import com.kyc.kycxkplaygame.PlayGameApplication;
import com.kyc.kycxkplaygame.greendao.LevelPassBean;
import com.kyc.kycxkplaygame.greendao.PropInfoBean;
import com.kyc.kycxkplaygame.greendao.UserInfoGdBean;
import com.kyc.kycxkplaygame.greendao.manager.LevelInfoManager;
import com.kyc.kycxkplaygame.greendao.manager.PropInfoManager;
import com.kyc.kycxkplaygame.greendao.manager.UserInfoManager;
import com.kyc.kycxkplaygame.util.SharedPreferencesUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/7/9.
 */

public class GetPropLevelUtils {
    private static GetPropLevelUtils instance;

    public static GetPropLevelUtils getInstance() {
        if (instance == null) {
            instance = new GetPropLevelUtils();
        }
        return instance;
    }

    public String getUserName(){
        return (String) SharedPreferencesUtils.getParam(PlayGameApplication.getContext(), "phone", "");
    }

    public String getPropNum(String propName) {
        List<PropInfoBean> propTili = PropInfoManager.getInstance().getInfoPropname(propName);
        if (propTili.size() > 0) {
           // Log.i("KYCC", "size === " + propTili.size() + ", name = " + propTili.get(0).getPropName() + " num === " + propTili.get(0).getPropNum());
            PropInfoBean propInfoBean = propTili.get(0);
            return String.valueOf(propInfoBean.getPropNum());
        } else {
            return "0";
        }
    }

    public Long getUserID(){
        List<UserInfoGdBean> infoGdBeen = UserInfoManager.getInstance().getInfoUserID(getUserName());
        return infoGdBeen.get(0).getId();
    }

    public void updatePropNum(String propName, int proNum) {
        List<PropInfoBean> propTili = PropInfoManager.getInstance().getInfoPropname(propName);
        if (propTili.size() > 0) {
            final PropInfoBean propInfoBean = new PropInfoBean();
            PropInfoBean propInfo = propTili.get(0);
            if (propInfo.getPropName().equals(propName)) {
                propInfoBean.setId(propInfo.getId());
                propInfoBean.setUserName(propInfo.getUserName());
                propInfoBean.setPropName(propInfo.getPropName());
                propInfoBean.setPropNum(proNum);
                new Thread() {
                    @Override
                    public void run() {
                        PropInfoManager.getInstance().updatePropInfo(propInfoBean);
                    }
                }.start();
            }
        }
    }

    public void updateNoFriutPropNum(String propName, int proNum) {
        List<PropInfoBean> propTili = PropInfoManager.getInstance().getInfoPropname(propName);
        if (propTili.size() > 0) {
            final PropInfoBean propInfoBean = new PropInfoBean();
            PropInfoBean propInfo = propTili.get(0);
            if (propInfo.getPropName().equals(propName)) {
                propInfoBean.setId(propInfo.getId());
                propInfoBean.setUserName(propInfo.getUserName());
                propInfoBean.setPropName(propInfo.getPropName());
                propInfoBean.setPropNum(proNum + propInfo.getPropNum());
                new Thread() {
                    @Override
                    public void run() {
                        PropInfoManager.getInstance().updatePropInfo(propInfoBean);
                    }
                }.start();
            }
        }
    }

    public int getLevel() {
        List<UserInfoGdBean> infoUserLevel = UserInfoManager.getInstance().getInfoUserLevel();
        //Log.i("KYCC", "level.size ==== " + infoUserLevel.size());
        if (infoUserLevel.size() > 0) {
            UserInfoGdBean infoGdBean = infoUserLevel.get(0);
            return Integer.parseInt(infoGdBean.getLevel());
        } else {
            return 0;
        }
    }

    public void updateLevel(String level) {
        List<UserInfoGdBean> infoUserLevel = UserInfoManager.getInstance().getUserInfo();
        if (infoUserLevel.size() > 0) {
            UserInfoGdBean gdBean = infoUserLevel.get(0);
            final UserInfoGdBean userInfoGdBean = new UserInfoGdBean();
            userInfoGdBean.setUserphone(gdBean.getUserphone());
            userInfoGdBean.setId(gdBean.getId());
            if (Integer.parseInt(gdBean.getLevel()) < Integer.parseInt(level)) {
                userInfoGdBean.setLevel(level);
            } else {
                userInfoGdBean.setLevel(gdBean.getLevel());
            }
            new Thread() {
                @Override
                public void run() {
                    UserInfoManager.getInstance().updateUserInfo(userInfoGdBean);
                }
            }.start();
        }
    }

    public int getPassLevelGrape(String level) {
        List<LevelPassBean> infoLevelname = LevelInfoManager.getInstance().getInfoLevelname(level);
        if (infoLevelname.size() > 0) {
            return infoLevelname.get(0).getPutaoNum();
        } else {
            return 0;
        }
    }

    public int getPassLevelStar(String level) {
        List<LevelPassBean> infoLevelname = LevelInfoManager.getInstance().getInfoLevelname(level);
        if (infoLevelname.size() > 0) {
            return infoLevelname.get(0).getStarCount();
        } else {
            return 0;
        }
    }

    public int getGrepCount(){
        List<LevelPassBean> levelPassBeen = LevelInfoManager.getInstance().getUserInfo();
        int count = 0;
        if(levelPassBeen.size() > 0) {
            for (LevelPassBean bean: levelPassBeen) {
                count += bean.getPutaoNum();
            }
        }
        return count;
    }
}
